const express = require('express');
const router = express.Router();

const authController = require('../controllers/authController');
const { authenticateToken, checkAccountLock } = require('../middleware/auth');
const { 
  validateUserRegistration,
  validateUserLogin,
  handleValidationErrors
} = require('../middleware/validation');
const { body } = require('express-validator');

/**
 * @route   POST /api/v1/auth/register
 * @desc    用户注册
 * @access  Public
 */
router.post(
  '/register',
  validateUserRegistration,
  authController.register
);

/**
 * @route   POST /api/v1/auth/login
 * @desc    用户登录
 * @access  Public
 */
router.post(
  '/login',
  checkAccountLock,
  validateUserLogin,
  authController.login
);

/**
 * @route   POST /api/v1/auth/refresh
 * @desc    刷新访问令牌
 * @access  Public
 */
router.post(
  '/refresh',
  [
    body('refreshToken')
      .notEmpty()
      .withMessage('刷新令牌不能为空'),
    handleValidationErrors
  ],
  authController.refreshToken
);

/**
 * @route   POST /api/v1/auth/logout
 * @desc    用户登出
 * @access  Private
 */
router.post(
  '/logout',
  authenticateToken,
  [
    body('refreshToken')
      .optional()
      .isString()
      .withMessage('刷新令牌格式无效'),
    handleValidationErrors
  ],
  authController.logout
);

/**
 * @route   GET /api/v1/auth/me
 * @desc    获取当前用户信息
 * @access  Private
 */
router.get(
  '/me',
  authenticateToken,
  authController.getCurrentUser
);

/**
 * @route   PUT /api/v1/auth/change-password
 * @desc    修改密码
 * @access  Private
 */
router.put(
  '/change-password',
  authenticateToken,
  [
    body('currentPassword')
      .notEmpty()
      .withMessage('当前密码不能为空'),
      
    body('newPassword')
      .isLength({ min: 6 })
      .withMessage('新密码长度至少6个字符')
      .matches(/^(?=.*[a-zA-Z])(?=.*\d)/)
      .withMessage('新密码必须包含至少一个字母和一个数字'),
      
    body('confirmPassword')
      .custom((value, { req }) => {
        if (value !== req.body.newPassword) {
          throw new Error('确认密码与新密码不匹配');
        }
        return true;
      }),
      
    handleValidationErrors
  ],
  authController.changePassword
);

/**
 * @route   POST /api/v1/auth/forgot-password
 * @desc    忘记密码 - 发送重置链接
 * @access  Public
 */
router.post(
  '/forgot-password',
  [
    body('email')
      .isEmail()
      .withMessage('请输入有效的邮箱地址')
      .normalizeEmail(),
    handleValidationErrors
  ],
  authController.forgotPassword
);

/**
 * @route   POST /api/v1/auth/reset-password
 * @desc    重置密码
 * @access  Public
 */
router.post(
  '/reset-password',
  [
    body('token')
      .notEmpty()
      .withMessage('重置令牌不能为空'),
      
    body('newPassword')
      .isLength({ min: 6 })
      .withMessage('新密码长度至少6个字符')
      .matches(/^(?=.*[a-zA-Z])(?=.*\d)/)
      .withMessage('新密码必须包含至少一个字母和一个数字'),
      
    body('confirmPassword')
      .custom((value, { req }) => {
        if (value !== req.body.newPassword) {
          throw new Error('确认密码与新密码不匹配');
        }
        return true;
      }),
      
    handleValidationErrors
  ],
  authController.resetPassword
);

module.exports = router;